import Vue from 'vue';
import App from './App.vue';
import router from './router/index';
import store from './store/index';
import apiInstaller from '@/api';
import { verifyUser } from './utils/index';
import { getToken, setToken, removeToken } from '@/utils/auth';
import './plugins/element.js';
import './plugins/mint.js';
// 引入svg组件
import './icons/index.js';

Vue.use(apiInstaller);

Vue.config.productionTip = false;

// function isEmptyObject(obj: any) {
//   for (const key in obj) {
//     return false;
//   }
//   return true;
// }
const userCode = verifyUser();
// 全局导航钩子
router.beforeEach(async (to: any, from: any, next: any) => {
  // console.log('全局导航钩子to, from, next :', to, from);
  const adminToken = getToken();
  console.log('adminToken :', adminToken, store.state.user.token);
  // 判断该路由是否需要登录权限
  if (userCode) {
    // 通过vuex state获取当前的token是否存在
    // console.log('store.state.user :', store.state.user);
    // console.log('Boolean(store.state.user.token) :', Boolean(store.state.user.token));
    next();
    // if (adminToken) {
    //   await next();
    // } else {
    //   await store.dispatch('LoginByUsername', userCode).then((res) => {
    //     console.log('LoginByUsername 成功 :', res);
    //     next();
    //   }).catch((err) => {
    //     console.log('LoginByUsername 失败 :', err);
    //   });
    // }
  }
});

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app');
